package com.example.gzysmartcity.ui.fragment.home;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentHomeBinding;
import com.example.gzysmartcity.repository.model.SCNewsType;
import com.example.gzysmartcity.ui.adapter.BrAdapter;
import com.example.gzysmartcity.ui.adapter.NewsAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.ui.fragment.factory.FactoryFragment;
import com.example.gzysmartcity.ui.fragment.hosp.HospFragment;
import com.example.gzysmartcity.ui.fragment.sell.SellFragment;
import com.example.gzysmartcity.ui.fragment.traffic.BusListFragment;
import com.example.gzysmartcity.ui.fragment.travel.TravelFragment;
import com.example.gzysmartcity.viewmodel.HomeViewModel;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import org.jetbrains.annotations.NotNull;

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {

    @Override
    protected FragmentHomeBinding viewBinding() {
        return FragmentHomeBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }


    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        HomeViewModel homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        Banner<Object, BrAdapter> banner = b.getRoot().findViewById(R.id.banner);
        b.rvNews.setLayoutManager(new LinearLayoutManager(getContext()));
        homeViewModel.getNewsType();
        NewsAdapter newsAdapter = new NewsAdapter(R.layout.rv_news_item);
        b.rfHome.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                refreshLayout.closeHeaderOrFooter();
                ToastUtils.showShort("没有更多信息了");
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                homeViewModel.getNewsType();
                ToastUtils.showShort("刷新成功");
            }
        });
        //选择分类
        b.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                homeViewModel.selectType(tab.getPosition());
                newsAdapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        b.search.setOnClickListener(v -> jump(SearchFragment.class, null, "search"));
        homeViewModel.getStateLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
                    b.rfHome.closeHeaderOrFooter();
                    b.tab.removeAllTabs();
                    for (SCNewsType.DataBean type : homeViewModel.getTypesList()) {
                        b.tab.addTab(b.tab.newTab().setText(type.getName()));
                    }
                    b.tab.selectTab(b.tab.getTabAt(0));
                    b.rvNews.setAdapter(newsAdapter);
                    newsAdapter.setNewInstance(homeViewModel.getNewsList());
                    newsAdapter.setOnItemClickListener((adapter, view12, position) -> {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("html", homeViewModel.getContent(position));
                        jump(NewsXqFragment.class, bundle, "news");
                    });
                    banner.setAdapter(new BrAdapter(homeViewModel.getBannerImgUrlList()))
                            .addBannerLifecycleObserver(this)
                            .setLoopTime(2000)
                            .setIndicator(new CircleIndicator(getContext()));
                    break;
                case FAIL:
                    ToastUtils.showShort("请求失败");
                    b.rfHome.closeHeaderOrFooter();
                    break;
            }
        });
        b.tvHomeFactory.setOnClickListener(v1 -> jump(FactoryFragment.class, null, null));
        b.tvHomeHosp.setOnClickListener(v1 -> jump(HospFragment.class, null, null));
        b.tvHomeTraffic.setOnClickListener(v1 -> jump(BusListFragment.class, null, null));
        b.tvHomeSell.setOnClickListener(v1 -> jump(SellFragment.class, null, null));
        b.tvHomeTravel.setOnClickListener(v1 -> jump(TravelFragment.class, null, null));
    }

    @Override
    protected String setTitle() {
        return null;
    }

    @Override
    protected boolean showBack() {
        return false;
    }

    @Override
    protected Boolean showBottom() {
        return true;
    }


}